﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

// Settings Class v.1.0
// by George Makarov
// Email: georgemakarov1@yandex.ru

namespace SettLib
{
    public class cSettings
    {
        [Serializable]
        public class cMainSett
        {
            public string SDFFilename;
            public cMainSett()
            {
                SDFFilename = string.Empty;
            }
        }
        [Serializable]
        public class cSyslogSett
        {
            public int Port;
            public int CacheSize;
            public int CachesCount;
            public int EventsQueueProcessingPeriod;
            public int FlushPeriod;
            public int DNSResolvePeriod;
            public int DNSResolveTimeout;
            public List<object[]> Interfaces;
            public cSyslogSett()
            {
                //default settings
                Port = 514;
                CacheSize = 500;
                CachesCount = 3;
                EventsQueueProcessingPeriod = 100;
                FlushPeriod = 900;
                DNSResolvePeriod = 600;
                DNSResolveTimeout = 2;
                Interfaces = new List<object[]>();
            }
        }
        [Serializable]
        public class cWebserverSett
        {
            public int Port;
            public string wwwPath;
            public int SessionTimeout;
            public int TempDirClearPeriod;
            public cWebserverSett()
            {
                //default settings
                Port = 80;
                wwwPath = string.Empty;
                SessionTimeout = 300;
                TempDirClearPeriod = 900;
            }
        }
        [Serializable]
        public class cData
        {
            public cMainSett Main;
            public cSyslogSett Syslog;
            public cWebserverSett Webserver;

            public cData()
            {
                Main = new cMainSett();
                Syslog = new cSyslogSett();
                Webserver = new cWebserverSett();
            }
        }

        private cData _Data;
        public cData Data
        {
            get
            { 
                return _Data; 
            }
            set
            {
                _Data = value;
            }
        }
        //private string StartupPath;


        public cSettings()
        {
            _Data = new cData();
            //StartupPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6);
        }

        public bool SaveSettings(string SettFileName)
        {
            bool Result = true;
            try
            {
                XmlSerializer writer = new XmlSerializer(_Data.GetType());
                StreamWriter file = new StreamWriter(SettFileName, false, Encoding.UTF8);
                writer.Serialize(file, _Data);
                file.Close();
            }
            catch
            {
                Result = false;
            }
            return Result;
        }

        public bool LoadSettings(string SettFileName)
        {
            bool Result = true;
            if (File.Exists(SettFileName))
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(_Data.GetType());
                    StreamReader file = new StreamReader(SettFileName, Encoding.UTF8);
                    _Data = (cData)reader.Deserialize(file);
                    file.Close();
                }
                catch
                {
                    Result = false;
                }
            }
            else
                Result = false;
            return Result;
        }
    }
}
